import '../../../tool/public_tool.dart';

/// FileName web_view_string
///
/// @Author lax
/// @Date 2024/4/2
///
/// @Description

class WebViewString {
  /// 获取执行JS代码
  static String? getEvaluateJS({String? innerHTML, String? runJSString}) {
    if (stringIsEmpty(runJSString) && stringIsEmpty(innerHTML)) {
      return null;
    }
    return '''
${bodyInnerHTMLJS(innerHTML: innerHTML) ?? ''}
document.body.style.fontSize = '2em';
document.querySelector('img').style.width = '100%';
${runJSString ?? ''}
''';
  }

  /// body内容替换
  static String? bodyInnerHTMLJS({String? innerHTML}) {
    if (stringIsEmpty(innerHTML)) {
      return null;
    }
    return "document.body.innerHTML = '${innerHTML ?? ''}';";
  }

  static String? innerHTML({
    String? text,
    String? imageUrl,
    String? videoUrl,
  }) {
    return '${textInnerHTML(text) ?? ''}${imageInnerHTML(imageUrl) ?? ''}${videoInnerHTML(videoUrl) ?? ''}';
  }

  /// 视频播放器
  static String? videoInnerHTML(String? videoUrl) {
    if (stringIsEmpty(videoUrl)) {
      return null;
    }
    return '<div style="display: flex; justify-content: center; align-items: center; height: 100vh;"> <video width="100%" controls autoplay> <source src="${videoUrl ?? ''}" type="video/mp4">您的浏览器不支持Video标签。</video> </div>';
  }

  /// 文字
  static String? textInnerHTML(String? text) {
    if (stringIsEmpty(text)) {
      return null;
    }
    text = text?.replaceAll('\r\n', '</p><p>');
    text = text?.replaceAll('\n', '</p><p>');
    return '<p></p><div>$text</div><p></p>';
  }

  /// 图片
  static String? imageInnerHTML(String? imageUrl) {
    if (stringIsEmpty(imageUrl)) {
      return null;
    }
    return '<p></p><div class="media-wrap image-wrap"><img class="media-wrap image-wrap" src="$imageUrl"/></div><p></p>';
  }
}
